
let Command = require('./command');

// 某个chain 在某个位置添加一个 sel node
class AddSelCommand extends Command {
    constructor(chain, localPos) {
        super();

        this.name = 'Add Sel Node';
        this.type = 'AddSel';
        this.localPos = localPos;

        this.setObj('chain',chain);

        this._lstAffect = [];
    }

    execute() {
        let chain = this.getObj('chain');
        let addedSel = chain.addSel(this.localPos);
        this.setObj('addedSel',addedSel);
        this.setAffectLst(this._lstAffect,addedSel);
    }

    undo() {

        let chain = this.getObj('chain');
        if (!chain) {
            throw new Error('this.chain is null!');
        }

        let addedSel = this.getObj('addedSel');
        if (!addedSel) {
            throw new Error('addedSel is null!');
        }

        this._lstAffect = this.getDepenItems(addedSel);
        chain.removeSel(addedSel);
        this.cleanAffectLst(this._lstAffect);
    }
}

module.exports = AddSelCommand;
